package whf.file.util;

import java.io.InputStream;
import java.util.Map;

import whf.framework.config.ApplicationConfig;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.resource.thread.ThreadPool;
import whf.framework.service.SpringService;
import whf.framework.util.BeanFactory;
import whf.framework.util.StringUtils;
import whf.framework.util.ThreadContext;
import whf.framework.util.images.ScaleProperty;

/**
 * 文件管理器，用于将文件写入硬盘
 * @author wanghaifeng
 * @create Nov 14, 2006 11:06:12 PM
 * 
 */
public class DiskFileManager extends SpringService implements FileManager{
	private static Log log = LogFactory.getLog(DiskFileManager.class);
	
	private ThreadPool pool;
	
	private boolean autoStartPool;
	
	private boolean poolStarted = false;
	
	/**
	 * 上传文件的根目录，如果没有配置，使用应用程序中全局配置的数据
	 * @property String:rootPath
	 */
	private String rootPath;
	
	public static FileManager getFileManager(){
		return (FileManager)BeanFactory.getBean(DiskFileManager.class);
	}
	
	public void setRootPath(String path){
		this.rootPath = path;
	}
	
	public String getRootPath(){
		return this.rootPath;
	}
	
	public void setAutoStartPool(boolean b){
		this.autoStartPool = b;
	}
	
	/* (non-Javadoc)
	 * @see whf.file.util.FileManager#save(org.apache.struts.upload.FormFile)
	 */
	public String save(InputStream data, String fileName){
		if(data == null) return null;
		String sessionId = ThreadContext.getSessionId();
		String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
		String dir = "";
		if(fileName.indexOf(System.getProperty("file.separator")) >=0 ) {
			dir = fileName.substring(0, fileName.indexOf(System.getProperty("file.separator")) + 1);
		}
		String resFileName = dir + sessionId + "-" + System.currentTimeMillis() + "." + ext;
		fileName = this.rootPath + resFileName;
		if(!this.poolStarted){
			this.pool.start();
			this.poolStarted = true;
		}
		this.pool.addThread(new FormFileSaveThread(data, fileName));
		return resFileName;
	}
	
	/* (non-Javadoc)
	 * @see whf.file.util.FileManager#saveImage(org.apache.struts.upload.FormFile, whf.framework.images.ScaleProperties)
	 */
	public String saveImage(InputStream data, String fileName, Map<String, ScaleProperty> props) {
		if(data == null) return null;
		String sessionId = ThreadContext.getSessionId();
		String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
		String resFileName = sessionId + "-" + System.currentTimeMillis() + "." + ext;
		fileName = this.rootPath + resFileName;
		if(!this.poolStarted){
			this.pool.start();
			this.poolStarted = true;
		}
		this.pool.addThread(new FormImageFileSaveThread(data, fileName, props));
		return resFileName;
	}

	/* (non-Javadoc)
	 * @see whf.file.util.FileManager#delete(java.lang.String)
	 */
	public void delete(String fileName){
		if(StringUtils.isEmpty(fileName)){
			return ;
		}
		if(!this.poolStarted){
			this.pool.start();
			this.poolStarted = true;
		}
		this.pool.addThread(new DeleteFileThread(fileName));
	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	public void afterPropertiesSet() throws Exception {
		super.afterPropertiesSet();
		this.pool = new ThreadPool(5);
		if(this.autoStartPool){
			pool.start();
			this.poolStarted = true;
		}
		if(StringUtils.isEmpty(this.rootPath)){
			this.rootPath = ApplicationConfig.getInstance().getUploadDirectory();
		}
	}

	public static void main(String[] args) throws Exception{
		System.out.println(DiskFileManager.getFileManager());
	}
}
